import sys
import time
import pyodbc

import generator as g
from db_utils import *

try:
    import psyco
    psyco.full()
except ImportError:
    print 'warning: cat\'t use psyco'


def fill_db(npages, uniform):
    cursor = get_cnxn().cursor()
    drop_tables(cursor)
    create_tables(cursor)
    y = None
    try:
        for i,x in enumerate(g.generate(nsites=100,
                                        npages_avg=npages,
                                        pdepth_avg=3,
                                        ndifflinks=30,
                                        nlinks_avg=2,
                                        ndiffkwds=100,
                                        nkwds_avg=7,
                                        nkwdsl_avg=2,
                                        uniform=uniform)):
            if not (i+1) % 100:
                print '.',
            if not (i+1) % 8000:
                print
            y = x
            add_to_db(cursor, x)
    except pyodbc.Error:
        print 'can\'t add:', y
    finally:
        cursor.execute('COMMIT')
        print

if __name__ == '__main__':
    uniform = True
    if len(sys.argv) == 3:
        uniform = sys.argv[2] == 'uniform'
    start = time.time()
    fill_db(float(sys.argv[1]), uniform)
    print 'see how fast it is: %g seconds' % (time.time() - start)

